package com.droidnow.model;

import android.app.Activity;
import android.content.res.AssetManager;
import android.util.Log;
import com.droidnow.common.XMLParser;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * User: tinnd
 * Date: 4/17/13
 * Time: 12:33 PM
 * To change this template use File | Settings | File Templates.
 */
public class Rss {
	private Activity activity;
	// All static variables
	public static final String URL = "http://media.tuoitre.vn/TTAudio.aspx";
	public static final String URL_BOOKS = "http://media.tuoitre.vn/BookMedia.aspx";
	//	public static final String URL = "http://api.androidhive.info/music/music.xml";
	// XML node keys
	public static final String KEY_SONG = "song"; // parent node
	public static final String KEY_ID = "id";
	public static final String KEY_TITLE = "title";
	public static final String KEY_ARTIST = "artist";
	public static final String KEY_DURATION = "duration";
	public static final String KEY_THUMB_URL = "thumb_url";
	public static final String KEY_AU_CONTENT = "CallbackContent";
	public static final String KEY_STATUS = "status";
	public static final String KEY_STATUS_SELECTED = "status.selected";
	public static final String KEY_STATUS_PLAYING = "status.playing";
	public static final String KEY_STATUS_NONE = "status.none";

	public static ArrayList<HashMap<String, String>> GetAudioNewsList() {
		ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

		XMLParser parser = new XMLParser();
		String xml = parser.getXmlFromUrl(Rss.URL + "?Cart_ctl00_phlCenter_ttaudio_calTTAudio_Callback_Param=19%2F4%2F2013");

		Document doc = parser.getDomElement(xml); // getting DOM element
		Node root = doc.getDocumentElement();

		Document docMain = parser.getDomElement(root.getTextContent());
		NodeList nl = docMain.getElementsByTagName("a");
//		looping through all song nodes <song>
		for (int i = 0; i < nl.getLength(); i++) {
			// creating new HashMap
			HashMap<String, String> map = new HashMap<String, String>();
			Element e = (Element) nl.item(i);


			// adding each child node to HashMap key → value
			map.put(Rss.KEY_ID, e.getAttribute("href").replace("javascript:PlayMedia(", "").replace(")", ""));
			map.put(Rss.KEY_TITLE, e.getTextContent());
			map.put(Rss.KEY_ARTIST, "");
			map.put(Rss.KEY_DURATION, e.getAttribute("href").replace("javascript:PlayMedia(", "").replace(")", ""));
			map.put(Rss.KEY_THUMB_URL, "");

			songsList.add(map);
		}
		return songsList;
	}

	public static ArrayList<HashMap<String, String>> GetBooksList() {
		ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

		XMLParser parser = new XMLParser();
		String xml = parser.getXmlFromUrl(Rss.URL_BOOKS + "?Cart_ctl00_phlCenter_newBook_calNewBook_Callback_Param=5");

		Document doc = parser.getDomElement(xml);
		Node root = doc.getDocumentElement();
		String html = root.getTextContent().replace("/?", "&#63;").replace("&", "&amp;");

		Document docMain = parser.getDomElement(String.format("<root>%s</root>", html));
		Log.e("DROID_____________DROR", "parser.getDomElement 2" + html);
		NodeList nl = docMain.getElementsByTagName("a");
//		looping through all book link nodes <song>
		for (int i = 0; i < nl.getLength(); i++) {
			// creating new HashMap
			HashMap<String, String> map = new HashMap<String, String>();
			Element e = (Element) nl.item(i);

			// adding each child node to HashMap key → value
			Log.e("DROID_____________DROR", "Class " + e.getAttribute("class"));
			if (e.getAttribute("class").equals("title4")) {
				String id = e.getAttribute("href").replace("BookDetail.aspx?BookID=", "");
				map.put(Rss.KEY_ID, id);
				map.put(Rss.KEY_TITLE, e.getTextContent());
				map.put(Rss.KEY_ARTIST, "");
				map.put(Rss.KEY_DURATION, id);
				map.put(Rss.KEY_THUMB_URL, "http://media.tuoitre.vn/ImgView.aspx?BookID=" + id);

				songsList.add(map);
			}
		}
		return songsList;
	}

	public static ArrayList<HashMap<String, String>> GetBookListOffline(Activity activity) {
		ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

		XMLParser parser = new XMLParser();
		String xml; // getting XML from URL
		AssetManager assetManager = activity.getAssets();
		InputStream input;
		try {
			input = assetManager.open("book_list.xml");
			int size = input.available();
			byte[] buffer = new byte[size];
			input.read(buffer);
			input.close();
			// byte buffer into a string
			xml = new String(buffer);
		} catch (IOException e) {
			xml = "";
			e.printStackTrace();
		}


		Document doc = parser.getDomElement(xml); // getting DOM element

		NodeList nl = doc.getElementsByTagName(Rss.KEY_SONG);
		// looping through all song nodes <song>
		for (int i = 0; i < nl.getLength(); i++) {
			// creating new HashMap
			HashMap<String, String> map = new HashMap<String, String>();
			Element e = (Element) nl.item(i);
			// adding each child node to HashMap key → value
			map.put(Rss.KEY_ID, parser.getValue(e, Rss.KEY_ID));
			map.put(Rss.KEY_TITLE, parser.getValue(e, Rss.KEY_TITLE));
			map.put(Rss.KEY_ARTIST, parser.getValue(e, Rss.KEY_ARTIST));
			map.put(Rss.KEY_DURATION, parser.getValue(e, Rss.KEY_DURATION));
			map.put(Rss.KEY_THUMB_URL, parser.getValue(e, Rss.KEY_THUMB_URL));

			songsList.add(map);
		}
		return songsList;
	}

	public static ArrayList<HashMap<String, String>> GetAudioBookList(String url) {
		ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
		XMLParser parser = new XMLParser();
//		Log.e("DROID-PLAY----------", "GetAudioBookList -2 ::"+url);
		String xml = parser.getXmlFromUrl(url);

		Document doc = parser.getDomElement(xml); // getting DOM element
//		Log.e("DROID-PLAY----------", "GetAudioBookList -1 ::"+xml);
		NodeList nl = doc.getElementsByTagName("REF");

		for (int i = 0; i < nl.getLength(); i++) {
			HashMap<String, String> map = new HashMap<String, String>();
			Element e = (Element) nl.item(i);
//			Log.e("DROID-PLAY----------", "GetAudioBookList 0");
			// adding each child node to HashMap key → value
			String href = e.getAttribute("HREF").replace("mms://", "http://");
			String title = href.substring(href.lastIndexOf('/') + 1).replace(".mp3", "");
			Log.e("DROID-PLAY----------", "GetAudioBookList 1 URL: " + href);
			map.put(Rss.KEY_ID, String.valueOf(i));
			map.put(Rss.KEY_TITLE, title);
			map.put(Rss.KEY_ARTIST, "");
			map.put(Rss.KEY_DURATION, "");
			map.put(Rss.KEY_THUMB_URL, href);
			map.put(Rss.KEY_STATUS, Rss.KEY_STATUS_NONE);
			Log.e("DROID-PLAY----------", "GetAudioBookList 2");

			songsList.add(map);
		}
		return songsList;
	}
}
